<template>
  <el-card class="layout-query">
    <el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item prop="name">
        <el-input v-model="state.queryForm.name" placeholder="参数键"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card>
    <el-space>
      <el-space>
        <el-button icon="Plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-space>
      <el-space>
        <el-button icon="Delete" plain type="danger" @click="deleteBatchHandle()">批量删除</el-button>
      </el-space>
    </el-space>
    <el-table
      v-loading="state.dataListLoading"
      show-overflow-tooltip
      :data="state.dataList"
      row-key="id"
      border
      class="layout-table"
      max-height="calc(100vh - var(--theme-header-height) - 40px - 225px)"
      @selection-change="selectionChangeHandle"
      :tree-props="{
        children: 'secondCodeDuckTagList',
      }"
    >
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column prop="name" label="标签名称" />
      <el-table-column prop="description" label="标签描述" align="center" />
      <el-table-column prop="createdAt" label="创建时间" align="center" />
      <el-table-column prop="updatedAt" label="更新时间" align="center" />
      <el-table-column label="操作" fixed="right" align="center" width="150">
        <template #default="{ row }">
          <el-button type="primary" link @click="addOrUpdateHandle(row as ListVO)">修改</el-button>
          <el-button type="primary" link @click="deleteBatchHandle((row as ListVO).id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="state.current"
      :page-sizes="state.pageSizes"
      :page-size="state.pageSize"
      :total="state.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <AddOrUpdate
      ref="addOrUpdateRef"
      @refresh-data-list="getDataList"
      :first-tag="state.dataList!.filter((data) => !data.parentId)"
    />
  </el-card>
</template>

<script setup lang="ts" name="SysTagIndex">
import { reactive, ref } from 'vue'

import { useCrud } from '@/hooks/useCrud'

import AddOrUpdate from './components/AddOrUpdate.vue'

import { IHooksOptions } from '@/hooks/useCrud/data.d'
import { ListVO, SearchDTO } from './data.d'

const state: IHooksOptions<SearchDTO, ListVO> = reactive({
  dataListUrl: '/codeDuckTag/list',
  deleteUrl: '/codeDuckTag/removeBatchByIds',
  primaryKey: 'id',
  queryForm: {
    name: '',
  },
})
const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle, reset } = useCrud<
  SearchDTO,
  ListVO
>(state)

const queryRef = ref()
const addOrUpdateRef = ref()
const addOrUpdateHandle = (row?: ListVO) => {
  addOrUpdateRef.value.init(row)
}
</script>
